package questionSolution.Question_283;

import java.util.Arrays;
import java.util.Scanner;
/*
易错点：
指针移动错误：慢指针slow直在交换非零元素之后移动，否则回导致非零元素位置错乱
处理空数组：需提前判断数组为null或长度为0的情况，避免空指针异常
如果只移动非零元素而不将原位置置为0，会导致数组中残留非零元素的副本
 */
public class Solution_1 {
    public void moveZeroes(int[] nums) {
        int slow = 0, fast = 0;
        while (fast < nums.length){
            if(nums[fast] != 0){
                int temp = nums[slow];
                nums[slow] = nums[fast];
                nums[fast] = temp;
                slow++;
            }
            fast++;
        }
    }

    static void main() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        Solution_1 sol = new Solution_1();
        sol.moveZeroes(arr);
        System.out.println(Arrays.toString(arr));
    }
}
